<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/4/23
 * Time: 16:34
 */

namespace App\Repos;
use App\Models\Car;
use App\Models\Merchant\GiftLog;
use App\Models\MyCar;
use Carbon\Carbon;

/**
 * 神秘商人库
 * 礼物类别
 * type
 *  1 金币，2 积分, 3 兑换某车辆， 4 实物, 5 虚拟商品
 * Class MerchantRepo
 * @package App\Repos
 */
class MerchantRepo
{
    protected $memberRepo;
    public function __construct(MemberRepo $memberRepo)
    {
        $this->memberRepo = $memberRepo;
    }
    
    public function addNewLog($user,$gift,$extra=null)
    {
        $newLog = new GiftLog();
        $newLog->user_id = $user->id;
        $newLog->merchant_gift_id=$gift->id;
        $newLog->category=$gift->category;
        $newLog->is_sended = 0;//延迟发放，待用户自行领取
        $newLog->extra=$gift->item;
        $newLog->save();
        return $newLog;
    }
    
    public function updateLottery($user,$log)
    {
        $is_sended = $is_used = 0;
        $message='';
        $code=0;
        switch ($log->category){
            case 1:
                //执行增加金币的操作
                if($this->memberRepo->incrementUserCredit($user,$log->extra)){
                    $is_sended = $is_used = 1;
                    MessageRepo::push([
                        'type'=>3,
                        'user_id'=>$user->id,
                        'to_user_id'=>$user->id,
                        'text'=>sprintf(config('words.messages.merchant_golds'),$log->extra),
                        'record_id'=>0,
                    ]);
                }
                
                break;
            case 2:
                if($this->memberRepo->incrementUserRemoteScore($user,$log->extra)){
                    $is_sended = $is_used = 1;
                    MessageRepo::push([
                        'type'=>3,
                        'user_id'=>$user->id,
                        'to_user_id'=>$user->id,
                        'text'=>sprintf(config('words.messages.merchant_credits'),$log->extra),
                        'record_id'=>0,
                    ]);
                }
                
                break;
            case 3:
                //用户有资格兑换某一车辆了
                //如果用户已有此车，则提示已拥有此车；否则直接送车给用户
                list($code,$message) = $this->exchangeCar($user,$log->extra);
                
                if($code < 0)
                    return [$code,$message];
    
                $is_sended = $is_used = 1;
                break;
            case 4:
                //领取实物,后台操作
                $is_sended = 1;
                break;
            case 5:
                //TODO::给用户发放虚拟物品-未定义
                $is_sended = 1;
                break;
        }
        
        $log->is_sended = $is_sended;
        $log->is_used = $is_used;
        $log->save();
        return [$code,$message];
    }
    
    
    private function exchangeCar($user,$car_id)
    {
        $car = Car::findOrFail($car_id);
        
        //检查是否已拥有此车
        //if(MyCar::where('uid',$user->id)->where('CarID',$car_id)->exists()){
        //    return [1,'用户已拥有此车'];
        //}
        $car_buyed = MyCar::where('uid',$user->id)->count();
    
        $max_cars_limit = \count(config('car.levels'));
        //检查总购买限制
        if($car_buyed >= $max_cars_limit){
            return [-1,'车位已满，需要出售车辆后才能领取车辆哦！'];
        }
        //检查用户拥有车的上限
        if($car_buyed >= $user->getCarLimit()){
            return [-1,'非常抱歉，您需要升级车库才能购买第'.($user->getCarLimit()+1).'辆车'];
        }
    
        $new_car = new MyCar();
        $new_car->uid = $user->id;
        $new_car->CarId = $car_id;
        $new_car->BuyTime = Carbon::now()->toDateTimeString();
        $new_car->BuyCredit = 0;
    
        $buyNewCar = $new_car->save();
    
        if($buyNewCar){
            MessageRepo::push([
                'type'=>3,
                'user_id'=>$user->id,
                'to_user_id'=>$user->id,
                'text'=>sprintf(config('words.messages.merchant_car'),$car->CarName),
                'record_id'=>0,
            ]);
            return [0,'兑换成功'];
        }
    
        return [-1,'兑换失败请重试'];
    }
    
    
}